﻿#include "taskeditdialog.h"


TaskEditDialog::TaskEditDialog(const QString& sTaskName,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TaskEditDialog),
    mpTaskItem(0)
{
    ui->setupUi(this);
    this->setObjectName("WidgetObjectTaskEditDialog");
    if(sTaskName.isEmpty())
    {
        mcurTaskObjName = QString();
        this->setWindowTitle(tr("AddTaskDialog"));
        mnWorkMode = 0;
        ui->pushButton_AddTask->setText(tr("AddTask"));
        ui->pushButton_AddAndExit->setText(tr("Add And Exit"));
    }
    else
    {
        mcurTaskObjName = sTaskName;
        this->setWindowTitle(tr("EditTaskDialog"));
        mnWorkMode = 1;
        ui->pushButton_AddTask->setText(tr("EditTask"));
        ui->pushButton_AddAndExit->setText(tr("Apply And Exit"));
    }

//    QDir settingDir(QDir::currentPath());
//    settingDir.cdUp();
//    settingDir.cd("config/FY4TaskOptimization");
//    QSettings settings(settingDir.absoluteFilePath("setting.ini"),QSettings::IniFormat);
//    settings.beginGroup("InstrumentBase");
//    QString sIns = settings.value("instrument").toString();
//    if(sIns.isEmpty())
//    {
//        sIns = TaskManager::getInstance()->currentInstrument()->instrumentName();
//        settings.setValue("instrument",sIns);
//    }
    ui->lineEdit->setText(TaskManager::getInstance()->currentInstrument()->instrumentName());

    QStringList lstTypes = TaskManager::getInstance()->currentInstrument()->taskTypes();
    ui->listWidget->addItems(lstTypes);
    ui->listWidget->item(0)->setSelected(true);

    //settings.endGroup();

    mpFullDisk = new FullDiskForm(this);
    mpFullDisk->setCurrentMode(mnWorkMode);
    ui->stackedWidget->addWidget(mpFullDisk);

    mpRegionScan = new RegionScanForm(this);
    mpRegionScan->setCurrentMode(mnWorkMode);
    ui->stackedWidget->addWidget(mpRegionScan);

    mpMidRegion = new MidRegionGroupForm(this);
    mpMidRegion->setCurrentMode(mnWorkMode);
    ui->stackedWidget->addWidget(mpMidRegion);

    // 红外
    FastPointTaskForm* InfraredForm = new FastPointTaskForm(this);
    InfraredForm->setObjectName("WidgetObjectFastPointTaskForm-InfraredBackground");
    InfraredForm->setCurrentMode(mnWorkMode);
    ui->stackedWidget->addWidget(InfraredForm);

    // 黑体
    FastPointTaskForm* BlackbodyForm = new FastPointTaskForm(this);
    BlackbodyForm->setObjectName("WidgetObjectFastPointTaskForm-BlackbodyScan");
    BlackbodyForm->setCurrentMode(mnWorkMode);
    ui->stackedWidget->addWidget(BlackbodyForm);

    // 恒星
    FastPointTaskForm* StarScanForm = new FastPointTaskForm(this);
    StarScanForm->setObjectName("WidgetObjectFastPointTaskForm-StarScan");
    StarScanForm->setCurrentMode(mnWorkMode);
    ui->stackedWidget->addWidget(StarScanForm);

    // 冷空
    FastPointTaskForm* sPaceScanForm = new FastPointTaskForm(this);
    sPaceScanForm->setObjectName("WidgetObjectFastPointTaskForm-sPaceScan");
    sPaceScanForm->setCurrentMode(mnWorkMode);
    ui->stackedWidget->addWidget(sPaceScanForm);


    connect(ui->listWidget,SIGNAL(currentTextChanged(QString)),this,SLOT(slot_changeWidget(QString)));

    //如果是EditTask模式而不是AddTask模式
    if(mnWorkMode == 1)
    {
        TaskItem* pTask = TaskManager::getInstance()->getTask(sTaskName);

//        if(pTask->getName().contains("MidregionScan"))
//        {

//            //找到当前编辑的中尺度，是第几个中尺度任务
//            TaskManager::getInstance()->getcurMidNum();
//            ///TaskManager::getInstance()->setcurMidNum();
//        }
        if(pTask != NULL)
        {
            for(int i=0; i<ui->stackedWidget->count(); ++i){
                QWidget* pWidget = ui->stackedWidget->widget(i);

                //找到editTask对应的任务类型 将当前任务类型选中为当前更改的task。然后将对应的widget显示出来
                if(NULL != pWidget && pWidget->objectName().contains(pTask->getType(),Qt::CaseInsensitive))
                {
                    for(int j=0;j<ui->listWidget->count();++j)
                    {
                        ui->listWidget->item(j)->setHidden(true);
                        if(pTask->getName().compare( ui->listWidget->item(j)->text(),Qt::CaseInsensitive) == 0)
                        {
                            ui->listWidget->setCurrentRow(j);
                            ui->listWidget->item(j)->setHidden(false);
                           // break;
                        }
                    }

                    TaskEditor* pEditor = dynamic_cast<TaskEditor*>(pWidget);
                    if(NULL != pEditor)
                    {
                        pEditor->updateTask(pTask);
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    continue;
                }
            }
        }
    }
}

TaskEditDialog::~TaskEditDialog()
{
    delete ui;
}
void TaskEditDialog::setTaskItem(TaskItem* pTask)
{
    mpTaskItem = pTask;
}

void TaskEditDialog::slot_changeWidget(QString s)
{
    for(int i=0; i<ui->stackedWidget->count(); ++i)
    {
        if(ui->stackedWidget->widget(i)->objectName().contains(s,Qt::CaseInsensitive))
        {
            ui->stackedWidget->setCurrentIndex(i);

            TaskEditor* pEditor = dynamic_cast<TaskEditor* >(ui->stackedWidget->currentWidget());
            pEditor->setCurrentTask(s);

            break;
        }
        else
        {
            continue;
        }
    }
}
void TaskEditDialog::on_pushButton_AddTask_clicked()
{
    if(mnWorkMode == 0)// 添加模式
    {
        QString sTaskType = ui->listWidget->currentItem()->text();

        TaskItem * pTask = TaskManager::getInstance()->addTask(sTaskType);
        pTask->setType(ui->listWidget->currentItem()->text());

        TaskEditor* pEditor = dynamic_cast<TaskEditor* >(ui->stackedWidget->currentWidget());
        pEditor->generateTask(pTask);
        TaskManager::getInstance()->slot_updateTask(pTask->objectName(),QString(),QString());
    }
    else// 编辑模式
    {
        TaskEditor* pEditor = dynamic_cast<TaskEditor* >(ui->stackedWidget->currentWidget());
        pEditor->generateTask(NULL);
        TaskManager::getInstance()->slot_updateTask(mcurTaskObjName,QString(),QString());
    }

}

void TaskEditDialog::on_pushButton_AddAndExit_clicked()
{
    on_pushButton_AddTask_clicked();
    this->accept();
}
